// import parseTime, formatTime and set to filter
import { parseTime, formatTime } from '@/utils'
import dayjs from 'dayjs'
/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label
  }
  return time + label + 's'
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time)
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute')
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour')
  } else {
    return pluralize(~~(between / 86400), ' day')
  }
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ]
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol
    }
  }
  return num.toString()
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

/**
 * Upper case first char
 * @param {String} string
 * 规格过滤
 */
export function skuFilter(row) {
  const attr = JSON.parse(row)
  const str = attr.skuAttribute.map(item => item.value)
  return str.join(',')
}
// 发起人枚举 promoterType
// 出借人  CREDITOR_MEMBER  ->  0 
// 借款人  DEBTOR_MEMBER    ->  1 
export function filterPromoterType(code) {
  let str = "";
  switch (code) {
    case "CREDITOR_MEMBER":
      label = "出借人";
      break;
    case "DEBTOR_MEMBER":
      label = "借款人";
      break;
  }
  return label;
}
// 合同枚举转换
// 填写中 'FILLING'   ->  0  
// 签署中 'SIGNING'   ->  1  
// 已生效 'EFFECT'    ->  2  
// 已撤回 'WITHDRAWN' ->  3  
// 已拒签 'REFUSED'   ->  4  
// 已失效 'EXPIRED'   ->  5  

export function filterContractState(str) {
  let code = "";
  switch (str) {
    case "FILLING":
      code = "填写中";
      break;
    case "SIGNING":
      code = "签署中";
      break;
    case "EFFECT":
      code = "已生效";
      break;
    case "WITHDRAWN":
      code = "已撤回";
      break;
    case "REFUSED":
      code = "已拒签";
      break;
    case "EXPIRED":
      code = "已失效";
      break;
  }
  return code;
}

// 合同付款方式枚举 paymentType
// 阿里支付   ALIPAY      -> 0
// 微信支付   WECHAT_PAT  -> 1 
// 其他       OTHER       -> 2

// 合同转账状态枚举 transferStatus
// 未转账 NO  ->  0 
// 已转账 YES ->  1 
export function filterPaymentType(row) {
  const { transferStatus, paymentType } = row
  if (transferStatus == "YES") {
    return '已转账(补签)'
  } else {
    if (paymentType == "ALIPAY") {
      return '支付宝'
    } else {
      return '其他'
    }
  }
}
// 年利率
export function rateOfYear({ loanAmount, contractInterest, debtorTime, repaymentTime } = {}) {
  debtorTime = dayjs(parseTime(debtorTime, '{y}-{m}-{d}'))
  repaymentTime = dayjs(parseTime(repaymentTime, '{y}-{m}-{d}'))
  const range = (!repaymentTime || !debtorTime) ? 0 : (repaymentTime.diff(debtorTime, 'day') + 1)
  const radio = contractInterest / loanAmount / range * 360 * 100
  console.log(range, '11111111');
  return radio.toFixed(2)
}

